﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.Leo
{
    /// <summary>
    /// 待宰管理
    /// </summary>
    public class SlaughteredCreaetHandler:IRequestHandler<SlaughteredCreaetCommend, Result>
    {
        private readonly IBaseRepository<Slaughteredtes> Slaughteredtes;
        private readonly IMapper mapper;

        public SlaughteredCreaetHandler(IMapper mapper, IBaseRepository<Slaughteredtes> Slaughteredtes)
        {
            this.mapper = mapper;
            this.Slaughteredtes = Slaughteredtes;
        }

        public async Task<Result> Handle(SlaughteredCreaetCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Slaughteredtes clients = mapper.Map<Slaughteredtes>(request);
                var res = Slaughteredtes.Add(clients);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
